yjx.controller('orgIntroController', ['$rootScope', '$scope', '$fetch', '$stateParams', 'modalService', '$timeout', '$init', 'htmlHandler', function ($rootScope, $scope, $fetch, $stateParams, modalService, $timeout, $init, htmlHandler) {
    var id = $stateParams.id;
    //$scope.name = "jjjj"
    //$scope.bannerDetail = null;


    $scope.tabList = [{ id: 1, name: '公司简介' }, { id: 2, name: '科学家' }, { id: 3, name: '实验室' }, { id: 4, name: '机构资质' }, { id: 5, name: '优势产品' }, { id: 6, name: '合作机构' }]
    $scope.curTab = $scope.tabList[0];
    var curSwiper;
    var scientistSwiper;
    var initialSilde;
    var width = document.body.offsetWidth
    function buildMainSwiper() {
        $timeout(function () {
            var tabList = new Swiper('#tabList2', {
                slidesPerView: 3,
                slideToClickedSlide: true,
            });
            curSwiper = new Swiper('#orgSwiper', {
                direction: 'horizontal',
                //noSwiping : true,
                slider: width,
                on: {
                    slideNextTransitionStart: function () {
                        tabList.slideTo(curSwiper.activeIndex);
                        resetBg(curSwiper.activeIndex);
                        $timeout(function () {
                            $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                        })
                    },
                    slidePrevTransitionStart: function () {
                        tabList.slideTo(curSwiper.activeIndex - 1);
                        resetBg(curSwiper.activeIndex);
                        $timeout(function () {
                            $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                        })
                    }
                }
            })

            //curSwipe  r.controller.control = tabList;//curSwiper控制tabList，需要在tabList初始化后
        }, 1000)
    }


    /**navtab点击 */
    $scope.tabSelect = function (item, index) {
        $scope.curTab = item;
        curSwiper.slideTo(index, 500);
        resetBg(index)
    }
    function resetBg(index) {
        if (index == 1) {
            // if ($scope.curScientist && $scope.curScientist.background)
                // changeBg($scope.curScientist.background, '.m-Intro');
        } else {
            angular.element(document.querySelector('.m-Intro')).css({ 'background-image': '' });
        }
    }
    function changeBg(url, classs) {
        var classs = classs || '.orgScientist'
        var mIntro = angular.element(document.querySelector(classs)).css({ 'background-image': 'url(\'' + url + '\')', 'background-size': '100%', 'background-repeat': 'no-repeat' });
    }
    /**科学家 实验室 资质 产品 内嵌的swiper */
    function buildSwipers() {
        var qualsSwiper = new Swiper('#qualSwiper', {
            slidesPerView: 1.2,
        });
        var productSwiper = new Swiper('#productSwiper', {
            slidesPerView: 1.2,
        });
        var experSwiper = new Swiper('#experSwiper', {
            slidesPerView: 1.2,
        });
        scientistSwiper = new Swiper('#scientistSwiper', {
            slidesPerView: 'auto',
            spaceBetween: 20,
            centeredSlides: true,
            initialSlide: initialSilde,
            // loop: true,
            // loopedSlides: $scope.scientist.length,
            slideToClickedSlide: true,
            // on: {
            //     slideChangeTransitionEnd: function () {
            //         console.log(scientistSwiper.activeIndex)
            //         // $scope.curScientist = $scope.scientist[scientistSwiper.activeIndex];
            //         // if($scope.curScientist && $scope.curScientist.background){
            //         //      changeBg($scope.curScientist.background,'.m-Intro');
            //         // }
            // }
            // }

        })
    }
    /**科学家tab点击 */
    $scope.scientistSelect = function (item, index) {
        $scope.curScientist = item;
        $scope.curScientist.digest = $scope.curScientist.synopsis.replace(/\n/g, '</br>')
        console.log($scope.curScientist.synopsis.replace(/\n/g, '</br>'))
        if (item && item.background) {
            // changeBg(item.background, '.m-Intro');
        }

    }


    function getDetail() {
        $fetch.fetch(
            '/supplier/detailById',
            {
                params: {
                    id: id
                },
            }
        ).then(function (data) {
            $timeout(function () {
                if (data) {
                    data.describe = htmlHandler.fontsizeHandler(data.describe);
                    $scope.data = data;
                    $init.title($scope.data.name);
                    $scope.quals = data.zizhi;
                    $scope.scientist = data.kexuejia;
                    $scope.experiments = data.shiyanshi;
                    $scope.products = data.youshi;
                    $scope.coops = data.danwei;
                    initialSilde = Math.ceil($scope.scientist.length / 2) - 1;
                    $scope.curScientist = $scope.scientist[initialSilde];
                    $scope.curScientist.digest = $scope.curScientist.synopsis.replace(/\n/g, '</br>')
                    $timeout(function () {
                        buildMainSwiper();
                        buildSwipers();
                    });
                } else {
                    modalService.mainMsgError("数据库中不存在该活动介绍");
                }
            })

        }
            ).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
    }
    if (id) {
        getDetail();
    }

}])